const UserModel = require('../model/user')
const jwt = require('../util/jwt')
const jwtConfig = require('../config/jwt')

exports.login = async (req, res, next) => {
    try {
        let result = await UserModel.login(req.fields)
        if (result.length === 1) {
            delete result[0].password
            delete result[0].isDel
            let token = await jwt.sign(result[0], jwtConfig.PrivateKey, { expiresIn: 60 * 60 * 24 * 7 })
            res.send({ status: 'success', msg: '用户登录成功', data: { user_info: result[0], token: token } })
        } else {
            res.send({ status: 'fail', msg: '用户登录失败', data: result.data })
        }
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '用户登录失败' })
    }
}

exports.checkNickName = async (req, res, next) => {
    try {
        let result = await UserModel.checkNickName(req.fields.nickName)
        if (result.length === 0) {
            res.send({ status: 'success', msg: '昵称可用', data: result })
        } else {
            res.send({ status: 'fail', msg: '昵称不可用' })
        }
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '检查昵称失败' })
    }
}

exports.checkUserPhone = async (req, res, next) => {
    try {
        let result = await UserModel.checkUserPhone(req.fields.user_phone)
        if (result.length === 0) {
            res.send({ status: 'success', msg: '电话可用', data: result })
        } else {
            res.send({ status: 'fail', msg: '电话不可用' })
        }
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '检查电话失败' })
    }
}

exports.register = async (req, res, next) => {
    try {
        let result = await UserModel.register(req.fields)
        // console.log(result);
        res.send({ status: 'success', msg: '用户注册成功', data: { id: result[0].insertId } })
    } catch (error) {
        // 这个错误是给开发者看的
        console.log(error);
        // 这个错误是给用户看的
        next({ status: 'fail', msg: '用户注册失败' })
    }
}

exports.uploadUserPhoto = async (req, res, next) => {
    if (req.files.user_photo.size === 0) return next({ status: 'fail', msg: '没有提交任何图片' })
    try {
        let result = await UserModel.uploadUserPhoto(req.files)
        // console.log(result);
        res.send({ status: 'success', msg: '头像上传成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '头像上传失败' })
    }
}

exports.editUser = async (req, res, next) => {
    try {
        let result = await UserModel.editUser(req.fields)
        res.send({ status: 'success', msg: '用户修改成功', data: { affectedRows: result[0].affectedRows } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '用户修改失败' })
    }
}

exports.findById = async (req, res, next) => {
    try {
        // console.log(req.params);
        let result = await UserModel.findById(req.params.id)
        res.send({ status: 'success', msg: '用户查找成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '用户查找失败' })
    }
}

exports.deleteById = async (req, res, next) => {
    try {
        // console.log(req.params);
        let result = await UserModel.deleteById(req.params.id)
        res.send({ status: 'success', msg: '用户删除成功', data: { affectedRows: result[0].affectedRows } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '用户删除失败' })
    }
}

exports.getListByPage = async (req, res, next) => {
    try {
        // console.log(req.query);
        let result = await UserModel.getListByPage(req.query)
        res.send({ status: 'success', msg: '用户列表查找成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '用户列表查找失败' })
    }
}